/**
 * 工具函数
 */

// 格式化时间
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

// 格式化数字
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 根据类型获取标记图标
const getMarkerIconByType = (type) => {
  switch(type) {
    case 'attraction':
      return '/images/marker-attraction.png';
    case 'restaurant':
      return '/images/marker-restaurant.png';
    case 'hotel':
      return '/images/marker-hotel.png';
    case 'start':
      return '/images/marker-start.png';
    default:
      return '/images/marker-default.png';
  }
}

// 获取POI类型中文名称
const getPoiTypeName = (type) => {
  switch(type) {
    case 'attraction':
      return '景点';
    case 'restaurant':
      return '餐厅';
    case 'hotel':
      return '住宿';
    case 'start':
      return '起点';
    default:
      return '地点';
  }
}

// 获取POI类型颜色
const getPoiTypeColor = (type) => {
  switch(type) {
    case 'attraction':
      return '#3366FF';
    case 'restaurant':
      return '#FF6600';
    case 'hotel':
      return '#33CC33';
    case 'start':
      return '#FF3333';
    default:
      return '#999999';
  }
}

// 根据天数筛选POI
const filterPoiByDay = (pois, day) => {
  if (!day) return pois;
  return pois.filter(poi => poi.day === day);
}

// 根据类型筛选POI
const filterPoiByType = (pois, type) => {
  if (!type) return pois;
  return pois.filter(poi => poi.type === type);
}

// 打开地图导航
const openMapNavigation = (longitude, latitude, name) => {
  wx.openLocation({
    longitude: parseFloat(longitude),
    latitude: parseFloat(latitude),
    name: name,
    scale: 18
  })
}

module.exports = {
  formatTime,
  getMarkerIconByType,
  getPoiTypeName,
  getPoiTypeColor,
  filterPoiByDay,
  filterPoiByType,
  openMapNavigation
}